const ctx = require.context(".", true, /\.js$/);
function rename(filename) {
  return filename
    .replace(/\//, "")
    .replace(/\./, "")
    .replace(/\.js/, "")
    .toLowerCase();
}
const apis = ctx.keys().map(filename => {
  if (filename !== "index.js") {
    return {
      name: rename(filename),
      component: ctx(filename).default
    };
  }
});

const ApiPlugin = Vue => {
  if (ApiPlugin.installed) return;
  ApiPlugin.installed = true;
  const $api = {};
  apis.forEach(api => {
    $api[api.name] = api.component;
  });
  Vue.prototype.$api = $api;
};

export default ApiPlugin;
